package com.ibeeking.found.upms.s.rest.service.impl;

import com.ibeeking.found.common.base.BaseDO;
import com.ibeeking.found.common.constants.RoleConstant;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.upms.s.rest.service.IShopRoleService;
import com.ibeeking.found.upms.s.rest.service.IShopUserRoleService;
import com.ibeeking.found.upms.service.common.bo.ShopRoleBO;
import com.ibeeking.found.upms.service.common.bo.ShopUserRoleBO;
import com.ibeeking.found.upms.service.common.dos.ShopRoleDO;
import com.ibeeking.found.upms.service.common.query.ShopRolePageQuery;
import com.ibeeking.found.upms.service.common.vo.ShopRolePageVO;
import com.ibeeking.found.upms.service.common.vo.ShopRoleVO;
import com.ibeeking.found.upms.service.mapper.ShopRoleMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ShopRoleServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-22 9:31
 **/
@Service
public class ShopRoleServiceImpl implements IShopRoleService {

    @Resource
    private ShopRoleMapper shopRoleMapper;
    @Resource
    private IShopUserRoleService userRoleServiceImpl;

    @Override
    public Long selectAdministratorId() {
        ShopRoleDO roleDO = shopRoleMapper.selectOne(new LambdaQueryWrapper<>(ShopRoleDO.class)
                .eq(ShopRoleDO::getCode, RoleConstant.ADMINISTRATOR)
        );
        return roleDO.getId();
    }

    @Override
    public Page<ShopRolePageVO> pageList(Long shopId, ShopRolePageQuery query) {
        LambdaQueryWrapper<ShopRoleDO> queryWrapper = new LambdaQueryWrapper<>(ShopRoleDO.class)
                .orderByAsc(ShopRoleDO::getSort).orderByDesc(BaseDO::getCreateTime)
                .eq(ShopRoleDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue())
                .eq(StringUtils.isNotBlank(query.getCode()), ShopRoleDO::getCode, query.getCode())
                .like(StringUtils.isNotBlank(query.getName()), ShopRoleDO::getName, query.getName());
        Page<ShopRoleDO> page = shopRoleMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), queryWrapper);
        Page<ShopRolePageVO> result = BeanUtil.convertPage(page, ShopRolePageVO.class);
        if (!Collections3.isEmpty(result.getRecords())) {
            List<Long> roleIds = result.getRecords().parallelStream().map(ShopRolePageVO::getId).collect(Collectors.toList());
            List<ShopUserRoleBO> shopUserRoleBos = userRoleServiceImpl.queryUserRoleByRoleIds(shopId, roleIds);
            Map<Long, List<ShopUserRoleBO>> userRoleMap = shopUserRoleBos.parallelStream().collect(Collectors.groupingBy(ShopUserRoleBO::getRoleId));
            result.getRecords().stream().forEach(tenantRolePageVO -> {
                tenantRolePageVO.setCount(null == userRoleMap.get(tenantRolePageVO.getId()) ? 0 : userRoleMap.get(tenantRolePageVO.getId()).size());
            });
        }
        return result;
    }

    @Override
    public ShopRoleVO view(Long id) {
        ShopRoleBO roleBO = getBean().queryById(id);
        return BeanUtil.convertBean(roleBO, ShopRoleVO.class);
    }

    @Override
    public ShopRoleBO queryById(Long id) {
        ShopRoleDO roleDO = shopRoleMapper.selectById(id);
        return BeanUtil.convertBean(roleDO, ShopRoleBO.class);
    }

    @Override
    public List<ShopRoleVO> allList() {
        List<ShopRoleDO> shopRoleDos = shopRoleMapper.selectList(new LambdaQueryWrapper<>(ShopRoleDO.class)
                .orderByAsc(ShopRoleDO::getSort)
                .orderByDesc(BaseDO::getCreateTime)
                .eq(ShopRoleDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue())
        );
        return JsonUtils.jsonToList(shopRoleDos, ShopRoleVO.class);
    }

    @Override
    public List<ShopRoleBO> queryByIds(List<Long> ids) {
        if (Collections3.isEmpty(ids)) {
            return Collections3.emptyList();
        }
        List<ShopRoleDO> shopRoleDos = shopRoleMapper.selectBatchIds(ids);
        List<ShopRoleBO> shopRoleBos = JsonUtils.jsonToList(shopRoleDos, ShopRoleBO.class);
        return shopRoleBos.stream().filter(s -> PublishStatusEnum.ENABLE.getValue().equals(s.getPublishStatus())).collect(Collectors.toList());
    }

    private IShopRoleService getBean() {
        return SpringBeanUtil.getBean(IShopRoleService.class);
    }
}
